//非均匀表面平面圆柱体效果不好
//无法响应光源相机位置变化，和固有色结合
// 1/和固有色贴图结合
Shader "matCap/vertex/Textured Add"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Matcap ("Matcap", 2D) = "white" {}
        _Color("Color",Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma fragmentoption ARB_precision_hint_fastest // 快速
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float2 cap:TEXCOORD1;
              float3 normal : NORMAL;

            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
              float3 normal : NORMAL;
                float3 worldNormal : NORMAL1;
                    float3 viewNormal : NORMAL2;
            };

            uniform sampler2D _MainTex;
            uniform sampler2D _Matcap;
            uniform float4 _MainTex_ST;
            uniform float4 _Color;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                float3 worldNorm =normalize(
                    unity_WorldToObject[0].xyz * v.normal.x
                + unity_WorldToObject[1].xyz * v.normal.y
                + unity_WorldToObject[2].xyz * v.normal.z
                );

                worldNorm =mul(UNITY_MATRIX_IT_MV,worldNorm) ;
                o.cap.xy = worldNorm.xy *0.5 + 0.5;
                return o;
            }

           

            fixed4 frag (v2f i) : SV_Target
            {
                // float3 normalColor = (i.viewNormal);
            //   return float4(normalColor,1.0);
                // sample the texture
                fixed4 tex = tex2D(_MainTex, i.uv);
                float4 mc =tex2D(_Matcap,i.cap.xy);

             #ifndef UNITY_COLORSPACE_GAMMA
                tex.rgb = LinearToGammaSpace(tex.rgb);
                mc.rgb = LinearToGammaSpace(mc.rgb);
                mc *= 2.0;
                mc = saturate(tex + mc - 1.0); // 什么意思?颜色加法 机器人材质和 阴影二次元贴图分级贴图结合
                mc.rgb = GammaToLinearSpace(mc.rgb);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, mc);
                return mc;
            #else
                mc.rgb tex.rgb + (mc.rgb * 2.0)-1.0;
                UNITY_APPLY_FOG(i.fogCoord, mc);
                return mc;
            #endif
            }
            ENDCG
        }
    }
}
